using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Rhino.net {
    /// <summary>
    /// the lexical analyzer
    /// </summary>
    public class TokenStream {
        private TextReader _reader;
        private Parser _parser;

        private Stack<int> _unreadChars = new Stack<int>();
        private List<Token> _tokens = new List<Token>();

        private Tokenizer _tokenizer = null;


        public TokenStream(Parser parser, TextReader reader, int lineNumber) : this(parser, lineNumber) {
            _reader = reader;
            _tokenizer = new Tokenizer(_reader);
        }

        public TokenStream(Parser parser, string sourceString, int lineNumber) : this(parser, lineNumber) {
            _reader = new StringReader(sourceString);
            _tokenizer = new Tokenizer(_reader);
        }

        private TokenStream(Parser parser, int lineNumber) {
            _parser = parser;
        }

    
        public Token GetNextToken() {
            int c = getChar();
            return _tokenizer.MakeToken(c);
        }

        public Token[] Tokenize {
            get {
                Token token = null;
                while((token = GetNextToken()) != Token.EOF) {
                    addToListOfTokens(token);
                }
                return _tokens.ToArray();
            }
        }

        private int getChar() {
            if (_unreadChars.Count > 0)
                return _unreadChars.Pop();
            return _reader.Read();
        }

        private void addToListOfTokens(Token token) {
            _tokens.Add(token);
        }

        private void unreadChar(int c) {
            _unreadChars.Push(c);
        }

        private bool matchChar(int charToMatch) {
            int c = getChar();
            bool result = c == charToMatch;
            if (!result)
                unreadChar(c);
            return result;
        }
    }

    public class Parser {}

  
}